//
//  YLReceivablesViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLReceivablesViewModel.h"
#import "YLGlobeHeader.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLReceivablesModel.h"
@implementation YLReceivablesViewModel

- (NSMutableArray<YLReceivablesGroup *> *)groups
{
    if (!_groups) {
        _groups = [NSMutableArray array];
        
    }
    return _groups;
}

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetBuyBackList", YLURL];
                
                NSMutableDictionary *params = input;
                NSInteger count = 100;
                params[@"MaxResultCount"] = @(count);
                
                NSInteger index = [params[@"PageIndex"] integerValue];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        if (index == 1) {
                            [weakSelf.groups removeAllObjects];
                        }
                        
                        YLReceivablesItem *item = [YLReceivablesItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        for (YLReceivablesModel *model in item.item) {
                            YLReceivablesGroup *group = [[YLReceivablesGroup alloc] init];
                            
//                            group = [[YLReceivablesGroup alloc] init];
                            group.title = model.periodsNumber;
                            if (model.buyBackStatus == 3) {
                                group.abandoned = YES;
                            } else {
                                group.abandoned = NO;
                            }
                            
                            NSArray *models = @[[YLTitleContentModel modelWithTitle:@"本期本金"
                                                                         content:[NSString stringWithFormat:@"%.2f", model.currentPrincipal] contentColor:YLLOrgColor],
                                             
                                             [YLTitleContentModel modelWithTitle:@"本期利息"
                                                                         content:[NSString stringWithFormat:@"%.2f", model.incomeAmount] contentColor:YLLOrgColor],
                                             
                                             [YLTitleContentModel modelWithTitle:@"本期收款额"
                                                                         content:[NSString stringWithFormat:@"%.2f", model.investmentIncomeSum]
                                                                    contentColor:YLLOrgColor],
                                             
                                             [YLTitleContentModel modelWithTitle:@"预期收款日期" content:model.paymentDate]];
                            
                            [group.models addObjectsFromArray:models];
                            
                            if (model.isLastNumber) {
                                [group.models insertObject:[YLTitleContentModel modelWithTitle:@"满减优惠额"
                                                                                       content:[NSString stringWithFormat:@"%.2f", model.additionalIncomeAmount] contentColor:YLLOrgColor] atIndex:2];
                            }
                            
                            [weakSelf.groups addObject:group];
                            
                        }
                        
                        [subscriber sendNext:nil];
                        
                        if (!(weakSelf.groups.count < item.count)) {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                    
                }];
                
                return nil;
            }];
            
        }];
    }
    return _requestCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

@end
